﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ColorClusteringSOM.Util
{


    class Reader
    {


        /*
          List<double[]> list8 = new List<double[]>();
            list8.Add(new double[] { 255, 0, 0 });   // Red
         */

        public static List<double[]> ListOfColors(String nameFile)
        {
            List<double[]> listColors = new List<double[]>();
            TextReader tr = new StreamReader(nameFile);

            String line = "";

            do
            {
                //Crimson	#DC143C	rgb(220,20,60)
                line = tr.ReadLine();

                if (line == null) 
                    break;
                for (int i = 0; i < line.Length; i++)
                {
                    char a = line.ElementAt(i);
                    if (line.ElementAt(i) == '#')
                    {
                        line = line.Remove(i, 12);
                    }
                }
                line = line.Trim('(', ')', ',','.');
                line = line.Replace('\t', ' ');
                //Console.WriteLine(line);

                String rgb = String.Empty;
                bool flag = false;
                for (int i = 0; i < line.Length; i++)
                {
                    char a = line.ElementAt(i);

                    if (flag)
                    {
                        rgb += a;
                    }

                    if (line.ElementAt(i) == ' ')
                    {
                        flag = true;
                    }

                   
                }
                String[] s = Regex.Split(rgb, ",");
                //Console.WriteLine();

                
                //Console.WriteLine(rgb);
                listColors.Add(new double[] { Int32.Parse(s[0]), Int32.Parse(s[1]), Int32.Parse(s[2]) });

              } while (line != "");
            Console.WriteLine("Pocet barev " + listColors.Count);
            return listColors;

        }

        /// <summary>
        /// Data pro intrudera
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static double[,] IntruderReader(String name)
        {
            double[,] matrix = CreateMatrix(5092, 42); //nastaveno rucne

            TextReader tr = new StreamReader(name);



            int counterRows = 0;
            String line = "";

            while (counterRows < 5092)
            {
                line = tr.ReadLine();

                if (line == "") break;
                String[] lineBreaker = Regex.Split(line, " ");
                /*lineBreaker
                 46:0,1 
                 62:0,2 
                 63:0,4               
                 */

                for (int s = 0; s < lineBreaker.Length; s++)
                {
                    String str = lineBreaker[s]; //46:0,1
                    if (str == "") break;
                    String[] valueBreaker = Regex.Split(str, ":");
                    //46
                    //0,1
                    int collum = Int32.Parse(valueBreaker[0]);
                    //Console.WriteLine(valueBreaker[1]);
                    String ssss = valueBreaker[1].Replace('.', ',');
                    
                    double value = Double.Parse(ssss);

                    matrix[counterRows, collum] = value;
                }



                counterRows++;
            }
            Console.WriteLine("NACTENO!!");
            return matrix;
        }

        public static String MedlarReader()
        {
            String name = "medlarDATA.txt";

            TextReader tr = new StreamReader(name);

            String allData = "";

            do{


                allData = tr.ReadToEnd();
                
                if (allData == "")
                {
                    break;
                }

                String[] s = Regex.Split(allData, " ");

                //String[] s = Regex.Split(line, " |:");
                //Attributs(s);
                

             } while (allData != "");

            return allData;
        }

        /// <summary>
        /// Jen pro zjisteni poctu sloupcu
        /// </summary>
        /// <param name="s"></param>
        public static void Attributs(String[] s)
        {
            double  max = Double.MinValue;
            
               

            for (int i = 0; i < s.Length-1; i++) // posledni radek je prazdny
            {
                double val = 0;
                 
                    String number = s[i];
                val = Double.Parse(number);
                
                 
                if (val > max)
                {
                    max = val;
                }
                
            }
                  

            Console.WriteLine("MAX="+max);
        }

        /// <summary>
        /// Pro midlera
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static double[,] DataMatrix(String name)
        {
            double[,] matrix = CreateMatrix(1033, 8707); //nastaveno rucne

            TextReader tr = new StreamReader(name);


            
            int counterRows = 0;
            String line = "";

            while ( counterRows < 1033)
            {
                line = tr.ReadLine();

                if (line == "") break;
                String[] lineBreaker = Regex.Split(line, " ");
                /*lineBreaker
                 46:0,1 
                 62:0,2 
                 63:0,4               
                 */

                for (int s = 0; s < lineBreaker.Length; s++)
                {
                    String str = lineBreaker[s]; //46:0,1
                    if (str == "") break;
                    String[] valueBreaker = Regex.Split(str, ":");
                    //46
                    //0,1
                    int collum = Int32.Parse(valueBreaker[0]);
                    double value = Double.Parse(valueBreaker[1]);

                    matrix[counterRows, collum] = value;
                }



                counterRows++;
            }
            Console.WriteLine("HEHE matrix has " + 1033*8707+" elements.");
            return matrix;

        }

        /// <summary>
        /// Jen udela pole a nastavi defaultni nuly pro vsechny pole
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="cols"></param>
        /// <returns></returns>
        private static double[,] CreateMatrix(int rows, int cols)
        {
            double[,] matrix = new double[rows , cols];

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    matrix[i, j] = 0;
                }
            }
            return matrix;
        }

        public static double[,] IntruderValidationReader(String name)
        {
            double[,] matrix = CreateMatrix(6890, 42); //nastaveno rucne

            TextReader tr = new StreamReader(name);
            Console.WriteLine(tr.ReadLine());


            int counterRows = 0;
            String line = "";

            while (counterRows < 6890)
            {
                line = tr.ReadLine();

                if (line == "") break;
                String[] lineBreaker = Regex.Split(line, "\t");
                /*lineBreaker
                 0
                 *                
                 */
                int counterCols = 0;
                for (int s = 0; s < lineBreaker.Length; s++)
                {
                   
                    double value = Double.Parse(lineBreaker[s].Replace('.',','));

                    matrix[counterRows, counterCols] = value;
                    counterCols++;
                }
                counterCols = 0;


                counterRows++;
            }
            //Console.WriteLine("NACTENO!!");
            return matrix;
        }

    }
}
